package com.org.es5.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.org.es5.api.entity.ActivityUser;
import com.org.es5.api.entity.MarketItem;
import com.org.es5.api.entity.MarketItemReq;
import com.org.es5.api.service.IndexService;
import com.org.es5.support.elasticserach.EsResult;
import com.org.es5.support.elasticserach.EsSearch;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther dh
 * @date 2021/12/23 18:09
 */
@RestController
public class IndexController {

	@Autowired
	private IndexService indexService;

	@RequestMapping("esTest")
	public List<Map<String, Object>> esTest() throws Exception {
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
		QueryBuilder selectTypeBuilder = QueryBuilders.termQuery("select_type", "item"); //  只统计请求访问量
		queryBuilder.must(selectTypeBuilder);
		searchSourceBuilder.query(queryBuilder);
		EsSearch.setShowLog(false);
		EsResult<Map<String, Object>> esResult = EsSearch.getEsResult(searchSourceBuilder, 1, 1, "market", "activity-user");
		List<Map<String, Object>> result = esResult.getEsResultList();
		System.out.println(esResult.getTotal());
		EsResult<ActivityUser> activityUserEsResult = EsSearch.getEsResult(searchSourceBuilder, 2, 1, "market", "activity-user", ActivityUser.class);
		System.out.println(JSONObject.toJSONString(activityUserEsResult));

		ActivityUser data = new ActivityUser();
		data.setId(7L);
		data.setSelectType("item7");
//		boolean bool = EsSearch.upsetDoc("market", "activity-user", data);
		boolean bool = EsSearch.updateDoc("market", "activity-user", data);
		return result;
	}


	@RequestMapping("initItemData")
	public String initItemData() throws Exception {
		List<MarketItem> list = indexService.list();
		EsSearch.updateIndexDocBulk("market", "item", list);
		return "success";
	}

	/**
	 * 查询活动数据
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("queryActivityData")
	public List<MarketItemReq> queryActivityData() throws Exception {
		List<MarketItemReq> resultList = new ArrayList<>();

		// 1、查询正在进行的活动
		List<Long> list = new ArrayList<>();
		list.add(101L);
		list.add(102L);
		// 2、查询正在进行的活动商品黑白名单
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
		QueryBuilder activityIdBuilder = QueryBuilders.termsQuery("activity_main_id", list);
		queryBuilder.must(activityIdBuilder);
		searchSourceBuilder.query(queryBuilder);
		EsSearch.setShowLog(true);
		EsResult<MarketItem> esResult = EsSearch.getEsResult(searchSourceBuilder, "market", "item", MarketItem.class);
		// 第三步：填充活动ID
		// 活动[101]设置商品白名单,活动[102]设置商品黑名单


		//第一种情况: 商品在活动[101]白名单里面,商品在活动[102]黑名单里面
		// 101:w ,102:b -》 可享受 101 不可享受 102
		List<String> tagList = new ArrayList<>();
		tagList.add("1");
		tagList.add("2");
		MarketItemReq marketItem = new MarketItemReq(99999L, 33333L, 2L, tagList);
		marketItem.fillActivity(esResult.getEsResultList());
		resultList.add(marketItem);

		//第二种情况: 商品在活动[101]白名单里面,商品不在活动[102]黑名单里面
		// 101:w ,102:w -》 可享受 101 可享受 102
		tagList = new ArrayList<>();
		tagList.add("3");
		tagList.add("4");
		marketItem = new MarketItemReq(123456L, 88888L, 4L, tagList);
		marketItem.fillActivity(esResult.getEsResultList());
		resultList.add(marketItem);

		//第三种情况: 商品不在活动[101]白名单里面,商品在活动[102]黑名单里面
		// 101:w ,102:w -》 不可享受 101 不可享受 102
		tagList = new ArrayList<>();
		tagList.add("3");
		tagList.add("4");
		marketItem = new MarketItemReq(123456L, 987345L, 22222L, tagList);
		marketItem.fillActivity(esResult.getEsResultList());
		resultList.add(marketItem);

		//第三种情况: 商品不在活动[101]白名单里面,商品不在活动[102]黑名单里面
		// 101:w ,102:w -》 不可享受 101 可享受 102
		tagList = new ArrayList<>();
		tagList.add("3");
		tagList.add("4");
		marketItem = new MarketItemReq(123456L, 987345L, 54321L, tagList);
		marketItem.fillActivity(esResult.getEsResultList());
		resultList.add(marketItem);

		return resultList;

	}

	@RequestMapping("queryItemActivity")
	public List<MarketItem> queryItemData() throws Exception {

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		BoolQueryBuilder whiteQueryBuilder = QueryBuilders.boolQuery();
		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

		/**
		 * item_store_id
		 */
		Long[] ids = new Long[]{99999L, 123456L};
		BoolQueryBuilder mustQueryBuilder = QueryBuilders.boolQuery();
		QueryBuilder selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		QueryBuilder selectTypeBuilder = QueryBuilders.termQuery("select_type", "item_store_id");
		QueryBuilder whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		queryBuilder.should(mustQueryBuilder);

		/**
		 * brand_classify_id
		 */
		ids = new Long[]{1L, 88888L};
		mustQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "brand_classify_id");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		queryBuilder.should(mustQueryBuilder);

		/**
		 * sale_classify_id
		 */
		ids = new Long[]{2L, 4L};
		mustQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "sale_classify_id");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		queryBuilder.should(mustQueryBuilder);

		/**
		 * tag
		 */
		ids = new Long[]{1L, 2L, 3L, 4L};
		mustQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "tag");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		queryBuilder.should(mustQueryBuilder);
		whiteQueryBuilder.must(queryBuilder);
		searchSourceBuilder.query(whiteQueryBuilder);


		BoolQueryBuilder blackQueryBuilder = QueryBuilders.boolQuery();


		/**
		 * item_store_id
		 */
		ids = new Long[]{99999L, 123456L};
		BoolQueryBuilder mustNotQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "item_store_id");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "b");
		mustNotQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		blackQueryBuilder.mustNot(mustNotQueryBuilder);
		/**
		 * brand_classify_id
		 */
		ids = new Long[]{1L, 88888L};
		mustNotQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "brand_classify_id");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "b");
		mustNotQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		blackQueryBuilder.mustNot(mustNotQueryBuilder);

		/**
		 * sale_classify_id
		 */
		ids = new Long[]{2L, 4L};
		mustNotQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "sale_classify_id");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustNotQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		blackQueryBuilder.mustNot(mustNotQueryBuilder);

		/**
		 * tag
		 */
		ids = new Long[]{1L, 2L, 3L, 4L};
		mustNotQueryBuilder = QueryBuilders.boolQuery();
		selectIdBuilder = QueryBuilders.termsQuery("select_id", ids);
		selectTypeBuilder = QueryBuilders.termQuery("select_type", "tag");
		whiteMerBlackWhiteTypeBuilder = QueryBuilders.termQuery("mer_black_white_type", "w");
		mustNotQueryBuilder.must(selectTypeBuilder).must(selectIdBuilder).must(whiteMerBlackWhiteTypeBuilder);
		blackQueryBuilder.mustNot(mustNotQueryBuilder);
		searchSourceBuilder.query(blackQueryBuilder);

		EsSearch.setShowLog(true);
		EsResult<MarketItem> esResult = EsSearch.getEsResult(searchSourceBuilder, "market", "item", MarketItem.class);
		return esResult.getEsResultList();


	}
}
